/**
 * Pager
 * V 1.0.0
 * 2014/12/5
 * <p>
 * Copyright © 2014 中国电信股份有限公司增值业务运营中心 版权所有
 * <p>
 * 所有文字、代码资料，版权均属中国电信股份有限公司增值业务运营中心
 * 所有，任何公司或个人未经本司协议授权，不得复制、下载、存储或公开
 * 显示。违者本公司将依法追究责任。
 */
package cn.com.manager.page;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * 分页自定义标记
 * <p>
 * Created by liutong on 2014/12/5.
 */
public class Pager extends TagSupport {
    static final Logger logger = LoggerFactory.getLogger(Pager.class);
    private static final long serialVersionUID = 2483578501713524732L;
    private Integer curPage;
    private Integer totalPage;
    private Integer pageSize;
    private Integer totalCount;
    private String formId;

    public int doStartTag() throws JspException {
        if (curPage == null || curPage < 1) {
            curPage = Integer.valueOf(1);
        }
        if (curPage > totalPage) {
            curPage = totalPage;
        }

        // 输出到页面的JS函数
        StringBuilder fun = new StringBuilder();
        // 输出到页面的代码
        StringBuilder str = new StringBuilder();

        // 请求地址
        String actionURL = (String) pageContext.getRequest().getAttribute(javax.servlet.RequestDispatcher.FORWARD_REQUEST_URI);

        fun.append("<script type='text/javascript'>")
                .append("function goPageNo(pageNo, pageSize){")
                .append("var f = document.getElementById('" + formId + "');")
                .append("f.action = '").append(actionURL).append("?pageNo=' + pageNo + '&pageSize=' + pageSize;")
                .append("f.method = 'POST';")
                .append("f.submit();")
                .append("}")
                .append("</script>");

        str.append("<div class='pagination'>");
        str.append("<select name='pageInfo.pageSize' style='width: 50px' onchange=\"goPageNo(" + curPage + ",this.options[this.selectedIndex].value);\"><option value='10' " + (pageSize == 10 ? "selected='selected'" : "") + ">10</option><option value='20' " + (pageSize == 20 ? "selected='selected'" : "") + ">20</option><option value='30' " + (pageSize == 30 ? "selected='selected'" : "") + ">30</option><option value='40' " + (pageSize == 40 ? "selected='selected'" : "") + ">40</option><option value='50' " + (pageSize == 50 ? "selected='selected'" : "") + ">50</option><option value='100' " + (pageSize == 100 ? "selected='selected'" : "") + ">100</option></select>&nbsp;&nbsp;&nbsp;&nbsp;");

        // 如果只有一页，则无需分页
        if (totalPage == 1) {
            str.append("<span class='current'>1</span>");
        } else {
            // 显示上一页与第一页
            if (curPage == 1) {
                str.append("<span class='disabled'><< 上一页</span>");
                str.append("<span class='current'>1</span>");
            } else {
                str.append("<a href='javascript:goPageNo(" + (curPage - 1) + "," + pageSize + ")'><< 上一页</a>");
                str.append("<a href='javascript:goPageNo(1," + pageSize + ")'>1</a>");
            }


            // 当前页超过5时第一页后面加点，因为中间相隔了第二页
            if (curPage - 4 > 1)
                str.append("<span class='gap'>...</span>");

            // v,v1分别代表中间页数的最小值和最大值,3表示显示当前页的前后三页
            int v = (curPage - 3) > 1 ? (curPage - 3) : 2;
            int v1 = (curPage + 3) < totalPage ? (curPage + 3) : totalPage - 1;
            if (v1 == totalPage) {
                v = totalPage - 10;
            } else if (v == 1 && v1 < totalPage) {
                v1 = totalPage > 10 ? 10 : totalPage;
            }

            //
            for (int i = v; i <= v1; i++) {
                if (curPage == i) { // 当前页要加粗显示
                    str.append("<span class='current'>");
                    str.append(i);
                    str.append("</span>");
                } else {
                    str.append("<a href='javascript:goPageNo(" + i + "," + pageSize + ")'>" + i + "</a>");
                }
            }

            if (curPage < totalPage - 4)
                str.append("<span class='gap'>...</span>");

            // 显示最后一页
            if (curPage.intValue() != totalPage.intValue()) {
                str.append("<a href='javascript:goPageNo(" + totalPage + "," + pageSize + ")'>" + totalPage + "</a>");
            } else {
                str.append("<span class='current'>" + curPage + "</span>");
            }

            // 显示下一页
            if (curPage.intValue() == totalPage.intValue()) {
                str.append("<span class='disabled'>下一页 >></span>");
            } else {
                str.append("<a href='javascript:goPageNo(" + (curPage + 1) + "," + pageSize + ")'>下一页 >></a>");
            }
        }

        //总记录数
        //b1:当前页开始数  b2:当前页结束数
        Integer b1 = 0;
        Integer b2 = 0;
        if (totalPage.intValue() == 1) {//如果只有一页
            b1 = 1;
            b2 = totalCount;
        } else {
            if (curPage.intValue() == totalPage.intValue()) {//最后一页
                b1 = (curPage - 1) * pageSize + 1;
                b2 = totalCount;
            } else {
                b1 = (curPage - 1) * pageSize + 1;
                b2 = curPage * pageSize;
            }
        }

        str.append("&nbsp;&nbsp;&nbsp;&nbsp;显示" + b1 + "到" + b2 + "，共" + totalCount + "条记录");
        str.append("</div>");


        try {
            pageContext.getOut().print(fun.append(str));
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return super.doStartTag();
    }

    public void setCurPage(Integer curPage) {
        this.curPage = curPage;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public void setFormId(String formId) {
        this.formId = formId;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }
}
